﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UM_SERVER.ProcessModel;
using UM_SERVER.Net;

namespace UM_SERVER.ProcessModel
{
    class TriggersProcessModel : Model
    {
        /// <summary>
        ///     向在线用户广播指定用户上线消息
        /// </summary>
        /// <param name="username"></param>
        public static void Boardcast_UserOnlineMessage(String username)
        {
            User.User OnlineUser = Globle.userManager.GetUser(username);
            if (OnlineUser != null)
            {
                //向所有在线用户广播
                foreach (IMetaData imeta in Globle.userManager.GetMembers())
                {
                    User.User user = (imeta as User.User);
                    if (user.Username != username)
                    {
                        byte[] Username = Encoding.Default.GetBytes(user.Username);
                        byte[] UsernameLength = BitConverter.GetBytes(Username.Length);
                        byte[] FriendName = Encoding.Default.GetBytes(username);
                        byte[] FriendNameLength = BitConverter.GetBytes(FriendName.Length);
                        byte[] IPAddress = Encoding.Default.GetBytes(OnlineUser.IpAddress);
                        byte[] IPAddressLength = BitConverter.GetBytes(IPAddress.Length);
                        byte[] TalkPort = BitConverter.GetBytes(OnlineUser.TalkPort);
                        byte[] VideoPort = BitConverter.GetBytes(OnlineUser.VideoPort);
                        byte[] VoicePort = BitConverter.GetBytes(OnlineUser.VoicePort);
                        byte[] FilePort = BitConverter.GetBytes(OnlineUser.TransferFilePort);
                        byte[] packagebody = new byte[Username.Length + FriendName.Length + IPAddress.Length + 28];
                        int Offset = 0;
                        Array.ConstrainedCopy(UsernameLength, 0, packagebody, Offset, 4);
                        Offset += 4;
                        Array.ConstrainedCopy(Username, 0, packagebody, Offset, Username.Length);
                        Offset += Username.Length;
                        Array.ConstrainedCopy(FriendNameLength, 0, packagebody, Offset, 4);
                        Offset += 4;
                        Array.ConstrainedCopy(FriendName, 0, packagebody, Offset, FriendName.Length);
                        Offset += FriendName.Length;
                        Array.ConstrainedCopy(IPAddressLength, 0, packagebody, Offset, 4);
                        Offset += 4;
                        Array.ConstrainedCopy(IPAddress, 0, packagebody, Offset, IPAddress.Length);
                        Offset += IPAddress.Length;
                        Array.ConstrainedCopy(TalkPort, 0, packagebody, Offset, 4);
                        Offset += 4;
                        Array.ConstrainedCopy(VideoPort, 0, packagebody, Offset, 4);
                        Offset += 4;
                        Array.ConstrainedCopy(VoicePort, 0, packagebody, Offset, 4);
                        Offset += 4;
                        Array.ConstrainedCopy(FilePort, 0, packagebody, Offset, 4);
                        Offset += 4;
                        //得到协议
                        byte[] protocol1 = ProtocolFactory.ProtocolFactory.GetProtocol("Boardcast_UserOnlineMessage",
                                                                                      (int)Packet.Protocol.OUT_NetworkCommand.BOARDCAST_USERONLINE_MESSAGE,
                                                                                      (int)Packet.Protocol.NetworkProtocol.Base,
                                                                                      packagebody,
                                                                                      ProtocolFactory.ProtocolFactory.ACK.No,
                                                                                      ProtocolFactory.ProtocolFactory.ENCRYPT.No, "");
                        NetControl.Send(protocol1, user.TalkPort, user.IpAddress);
                    }
                }
            }
        }

        public static void Boardcast_DeleteFriendMessage(String username, String Friendname, UM_SERVER.Triggers.TriggerObject.DeleteFriendTrigger.DELETE_FRIEND_STATE DeleteFriendState)
        {
            User.User user = Globle.userManager.GetUser(username);
            if (user != null)
            {
                byte[] Username = Encoding.Default.GetBytes(username);
                byte[] UsernameLength = BitConverter.GetBytes(Username.Length);
                byte[] FriendName = Encoding.Default.GetBytes(Friendname);
                byte[] FriendNameLength = BitConverter.GetBytes(FriendName.Length);
                byte[] packagebody = new byte[Username.Length + FriendName.Length + 9];
                int Offset = 0;
                Array.ConstrainedCopy(UsernameLength, 0, packagebody, Offset, 4);
                Offset += 4;
                Array.ConstrainedCopy(Username, 0, packagebody, Offset, Username.Length);
                Offset += Username.Length;
                Array.ConstrainedCopy(FriendNameLength, 0, packagebody, Offset, 4);
                Offset += 4;
                Array.ConstrainedCopy(FriendName, 0, packagebody, Offset, FriendName.Length);
                Offset += FriendName.Length;
                byte[] DeleteState = null;
                if (DeleteFriendState == UM_SERVER.Triggers.TriggerObject.DeleteFriendTrigger.DELETE_FRIEND_STATE.SUCESSFUL)
                {
                    DeleteState = BitConverter.GetBytes(true);
                }
                else
                {
                    DeleteState = BitConverter.GetBytes(false);
                }
                Array.ConstrainedCopy(DeleteState, 0, packagebody, Offset, 1);
                Offset += 1;
                //得到协议
                byte[] protocol1 = ProtocolFactory.ProtocolFactory.GetProtocol("DeleteFriend",
                                                                              (int)Packet.Protocol.OUT_NetworkCommand.DELETE_FRIEND,
                                                                              (int)Packet.Protocol.NetworkProtocol.Base,
                                                                              packagebody,
                                                                              ProtocolFactory.ProtocolFactory.ACK.No,
                                                                              ProtocolFactory.ProtocolFactory.ENCRYPT.No, "");
                NetControl.Send(protocol1, user.TalkPort, user.IpAddress);
            }
        }
    }
}
